package com.northpool.operator.analysis.utils;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.PrecisionModel;

import java.util.ArrayList;
import java.util.List;

public class HexagonalGridOperator {
    private static final double EARTH_RADIUS = 6371000;

    private static final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);

    /**
     * 生成六边形格网
     *
     * @param minX       左下角经度
     * @param minY       左下角纬度
     * @param maxX       右上角经度
     * @param maxY       右上角纬度
     * @param sideLength 六边形边长，单位是米
     * @return 六边形格网
     */
    public static List<Polygon> generateHexagonGrid(double minX, double minY, double maxX, double maxY, double sideLength) {
        List<Polygon> hexagons = new ArrayList<>();

        double width = sideLength * Math.sqrt(3);
        double height = sideLength * 1.5;

        int cols = (int) Math.ceil((maxX - minX) / width);
        int rows = (int) Math.ceil((maxY - minY) / height);

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                double x = minX + col * width + (row % 2 == 1 ? width / 2 : 0);
                double y = minY + row * height;

                Coordinate[] coordinates = new Coordinate[7];
                coordinates[0] = new Coordinate(x + sideLength * Math.cos(0), y + sideLength * Math.sin(0));
                coordinates[1] = new Coordinate(x + sideLength * Math.cos(Math.PI / 3), y + sideLength * Math.sin(Math.PI / 3));
                coordinates[2] = new Coordinate(x + sideLength * Math.cos(2 * Math.PI / 3), y + sideLength * Math.sin(2 * Math.PI / 3));
                coordinates[3] = new Coordinate(x + sideLength * Math.cos(Math.PI), y + sideLength * Math.sin(Math.PI));
                coordinates[4] = new Coordinate(x + sideLength * Math.cos(4 * Math.PI / 3), y + sideLength * Math.sin(4 * Math.PI / 3));
                coordinates[5] = new Coordinate(x + sideLength * Math.cos(5 * Math.PI / 3), y + sideLength * Math.sin(5 * Math.PI / 3));
                coordinates[6] = new Coordinate(x + sideLength * Math.cos(0), y + sideLength * Math.sin(0));

                Polygon hexagon = geometryFactory.createPolygon(coordinates);
                hexagons.add(hexagon);
            }
        }

        return hexagons;
    }

    public double sideLengthInDegrees(double sideLength, double latitude) {
        // 将米转换为弧度
        double radians = sideLength / EARTH_RADIUS;

        // 将弧度转换为度数
        double degrees = Math.toDegrees(radians);

        // 考虑纬度的影响
        double cosineLatitude = Math.cos(Math.toRadians(latitude));
        return degrees / cosineLatitude;
    }

    public static void main(String[] args) {
        double minX = 0;
        double minY = 0;
        double maxX = 1000;
        double maxY = 1000;
        double sideLength = 100; // 边长单位是米

        List<Polygon> hexagons = generateHexagonGrid(minX, minY, maxX, maxY, sideLength);

        for (Polygon hexagon : hexagons) {
            System.out.println(hexagon);
        }
    }
}
